package com.teamall.web.app.controller;

import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.SpringAppContextUtil;
import com.teamall.framework.util.Utils;
import com.teamall.web.app.vo.FamilyHelpVO;
import com.teamall.web.app.vo.FamilyReplyVO;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.family.db.po.FamilyHelpPO;
import com.teamall.web.family.db.po.FamilyReplyPO;
import com.teamall.web.family.service.IFamilyHelpService;
import com.teamall.web.family.service.IFamilyReplyService;
import com.teamall.web.sys.db.po.SysUserPO;
import com.teamall.web.sys.db.po.SysUserTokenPO;
import com.teamall.web.sys.service.ISysUserService;
import com.teamall.web.sys.service.impl.SysUserTokenServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-01 23:23
 **/
@Controller
@RequestMapping("/app/help")
public class HelpController {
    @Autowired
    private IFamilyHelpService familyHelpService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IFamilyReplyService familyReplyService;
    @RequestMapping("/query")
    @ResponseBody
    public ResResult query(Integer userObjType, Integer userId, Integer userObjId, FamilyHelpPO po, Page<FamilyHelpPO> page) throws BaseAppException {
        ResResult resResult = new ResResult();
        if (Utils.isEmpty(userObjType) || (Utils.isEmpty(userObjType) && Utils.isEmpty(userId))) {
            resResult.setCode("7000");
            resResult.setMsg("参数错误");
            return resResult;
        }
        if (po.getHelpState() <= -1) {
            po.setHelpState(null);
        }
        Integer paramUserId = Utils.notEmpty(userId) ? userId : userObjId;
        Page<FamilyHelpVO> voPage = new Page<FamilyHelpVO>();
        List<FamilyHelpVO> voList = new ArrayList<FamilyHelpVO>();
        voPage.setResultList(voList);
        Integer notSolveCount = familyHelpService.selectByUser(userObjType, paramUserId, po, page);
        BeanUtils.copyProperties(page, voPage);
        List<FamilyHelpPO> resultList = page.getResultList();
        for (FamilyHelpPO familyHelpPO : resultList) {
            FamilyHelpVO vo = new FamilyHelpVO();
            BeanUtils.copyProperties(familyHelpPO, vo);
            SysUserPO sysUserPO = sysUserService.selectByPrimaryKey(familyHelpPO.getHelpStaff());
            Integer aidMainStaff = sysUserService.getAidMainStaffByUserObjId(sysUserPO.getUserObjId());
            // 查询用户的familyId
            // 根据FAMILYID获取
            vo.setAidMainStaff(aidMainStaff);
            vo.setUserName(sysUserPO.getUserName());
            vo.setHeadImgUrl(sysUserPO.getHeadImgUrl());
            vo.setUser_id(sysUserPO.getUserObjId());
            voList.add(vo);
        }
        voPage.setResultList(voList);
        voPage.setNotSolveCount(notSolveCount);
        resResult.setRspData(voPage);
        return resResult;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResResult add(HttpServletRequest request, String helpContent) throws BaseAppException {
        String token = request.getHeader("token");
        Integer userInfoId = this.getUserByToken(token).getUserId();
        ResResult resResult = new ResResult();
        if (Utils.isEmpty(helpContent)) {
            resResult.setCode("7000");
            resResult.setMsg("参数错误");
            return resResult;
        }
        SysUserPO sysUserPO = sysUserService.selectByPrimaryKey(userInfoId);
        FamilyHelpVO familyHelpVO = new FamilyHelpVO();
        FamilyHelpPO po = new FamilyHelpPO();
        po.setHelpDate(new Date());
        po.setHelpContent(helpContent);
        po.setHelpState(0);
        po.setHelpStaff(userInfoId);
        po.setTownId(sysUserPO.getTownId());
        po.setVillageId(sysUserPO.getVillageId());
        int poId = familyHelpService.add(po);
        po.setHelpId(poId);
        BeanUtils.copyProperties(po, familyHelpVO);
        familyHelpVO.setHeadImgUrl(sysUserPO.getHeadImgUrl());
        familyHelpVO.setUserName(sysUserPO.getUserName());
        resResult.setRspData(familyHelpVO);
        return resResult;
    }

    @RequestMapping("/reply")
    @ResponseBody
    public ResResult reply(HttpServletRequest request, Integer helpId, String feedbackContent) throws BaseAppException {
        String token = request.getHeader("token");
        Integer userInfoId = this.getUserByToken(token).getUserId();
        ResResult resResult = new ResResult();
        if (Utils.isEmpty(feedbackContent) || Utils.isEmpty(helpId)) {
            resResult.setCode("7000");
            resResult.setMsg("参数错误");
            return resResult;
        }
        int result = familyReplyService.reply(helpId, feedbackContent, userInfoId);
        if (result == -1) {
            resResult.setCode("1000");
            resResult.setMsg("求助信息不存在");
            return resResult;
        }
        return resResult;
    }

    @RequestMapping("/solve")
    @ResponseBody
    public ResResult solve(Integer helpId,String feedBackContent) throws BaseAppException {
        ResResult resResult = new ResResult();
        if (Utils.isEmpty(helpId)) {
            resResult.setCode("7000");
            resResult.setMsg("参数错误");
        }
        FamilyHelpPO po = familyHelpService.selectByPrimaryKey(helpId);
        if (Utils.isEmpty(po)) {
            resResult.setCode("1000");
            resResult.setMsg("求助信息不存在");
            return resResult;
        }
        po.setHelpState(1);

        familyHelpService.update(po);
        return resResult;
    }

    @RequestMapping("/replyList")
    @ResponseBody
    public ResResult replyList(HttpServletRequest request,Integer helpId, Page<FamilyReplyPO> page) throws BaseAppException {
        String token = request.getHeader("token");
        Integer userInfoId = this.getUserByToken(token).getUserId();
        ResResult resResult = new ResResult();
        if (Utils.isEmpty(helpId)) {
            resResult.setCode("7000");
            resResult.setMsg("参数错误");
        }
        FamilyReplyPO po = new FamilyReplyPO();
        po.setHelpId(helpId);
        familyReplyService.selectByArgAndPage(po, page);
        Page<FamilyReplyVO> voPage = new Page<FamilyReplyVO>();
        BeanUtils.copyProperties(page, voPage);
        List<FamilyReplyPO> familyReplyPOList = page.getResultList();
        List<FamilyReplyVO> familyReplyVOList = new ArrayList<FamilyReplyVO>();
        for (FamilyReplyPO replyPO : familyReplyPOList) {
            FamilyReplyVO familyReplyVO = new FamilyReplyVO();
            BeanUtils.copyProperties(replyPO, familyReplyVO);
            SysUserPO sysUserPO = sysUserService.selectByPrimaryKey(replyPO.getUserId());
            familyReplyVO.setHeadImgUrl(sysUserPO.getHeadImgUrl());
            familyReplyVO.setUserName(sysUserPO.getUserName());
            familyReplyVOList.add(familyReplyVO);
            if(replyPO.getUserId() == userInfoId){
                familyReplyVO.setMe(true);
            }else{
                familyReplyVO.setMe(false);
            }
        }
        voPage.setResultList(familyReplyVOList);
        resResult.setRspData(voPage);
        return resResult;
    }

    public SysUserTokenPO getUserByToken(String token) throws BaseAppException {
        SysUserTokenServiceImpl sysUserTokenService = SpringAppContextUtil.getBean(SysUserTokenServiceImpl.class);
        SysUserTokenPO sysUserTokenPO = sysUserTokenService.selectByToken(token);
        return sysUserTokenPO;
    }
}
